package com.eqdd.yiqidian.ui.mine.renwu;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.eqdd.yiqidian.LizhiActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.MyFragmentPagerAdapter;
import com.eqdd.yiqidian.annotation.DealCode;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.CodeRule;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.TabEntity;
import com.eqdd.yiqidian.rxbus2.Subscribe;
import com.eqdd.yiqidian.rxbus2.ThreadMode;
import com.eqdd.yiqidian.ui.mine.renwu.fragment.MyCreateRenwuFragment;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;
@DealCode(codes = {CodeRule.RENWU_FAQI})
public class ShowMyCreateRenwuActivity extends BaseActivity {
    private LizhiActivityCustom dataBinding;
    private List<String> titles = new ArrayList<>();
    private ArrayList<Fragment> fragments;

    @Subscribe(codes = {CodeRule.RENWU_FAQI},threadMode = ThreadMode.MAIN)
    public void refreshCode(){

    }
    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_lizhi);
        initTopTitleBar(View.VISIBLE, "我发布的任务");
//        if (GreenDaoUtil.getNum(CodeRule.RENWU_FAQI) != 0) {
//            GreenDaoUtil.setNum(CodeRule.RENWU_FAQI, 0);
//        }
    }

    @Override
    public void initData() {
        isRxbus=true;
        titles.add("待接受");
        titles.add("已接受");
        titles.add("已拒绝");
        titles.add("已完成");
    }

    @Override
    public void setView() {
        fragments = new ArrayList<>();
        ArrayList<CustomTabEntity> customTabEntities = new ArrayList<>();
        for (int i = 0; i < titles.size(); i++) {
            TabEntity tabEntity = new TabEntity(titles.get(i), 0, 0);
            customTabEntities.add(tabEntity);
            fragments.add(getFragment(i));
        }

        dataBinding.viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), titles, fragments));
        dataBinding.commonTabLayout.setTabData(customTabEntities);
        dataBinding.commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                dataBinding.viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        dataBinding.viewPager
                .addOnPageChangeListener(
                        new ViewPager.OnPageChangeListener() {
                            @Override
                            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                            }

                            @Override
                            public void onPageSelected(int position) {
                                dataBinding.commonTabLayout.setCurrentTab(position);
                            }

                            @Override
                            public void onPageScrollStateChanged(int state) {

                            }
                        }
                );
    }

    private Fragment getFragment(int status) {
        MyCreateRenwuFragment fragment = new MyCreateRenwuFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(Config.STATUS, status);

        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        fragments.get(dataBinding.viewPager.getCurrentItem()).onActivityResult(requestCode, resultCode, data);
    }
}
